import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.util.Scanner;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

public class shiyan3_3_cli {
    private static DatagramSocket mSocket;
    public static final String key = "jkl;POIU1234++==";
    public static final byte[] rand = {1,2,3,4,5,6,7,8};

    public static void main(String[] args) {
	Scanner scan = new Scanner(System.in);
        try {
            // 1.初始化DatagramSocket
            mSocket = new DatagramSocket();
            mSocket = new DatagramSocket();
            // 2.创建用于发送消息的DatagramPacket
            while(true) {
                String data = scan.nextLine();
                InetSocketAddress address = new InetSocketAddress("localhost", 8000);

		char[] password = key.toCharArray();
		PBEKeySpec pbks = new PBEKeySpec(password);
		SecretKeyFactory kf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
		SecretKey k = kf.generateSecret(pbks);
		Cipher cp = Cipher.getInstance("PBEWithMD5AndDES");
		PBEParameterSpec ps = new PBEParameterSpec(rand,1000);
		cp.init(Cipher.ENCRYPT_MODE,k,ps);
		byte[] inda = data.getBytes("UTF8");
		byte[] outda = cp.doFinal(inda);
		System.out.println(outda);

                DatagramPacket sendPacket = new DatagramPacket(outda, outda.length, address);
                // 3.向服务端发送消息
                mSocket.send(sendPacket);
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        } finally {
            if (mSocket != null) {
                mSocket.close();
            }
        }
	scan.close();
    }
    public static String funcByteToStr(byte[] buffer) {
        try {
            int length = 0;
            for (int i = 0; i < buffer.length; ++i) {
                if (buffer[i] == 0) {
                    length = i;
                    break;
                }
            }
            return new String(buffer, 0, length, "UTF-8");
        } catch (Exception e) {
            return "";
        }
    }

}
